package com.yunhe.authority.controller.company;

import com.yunhe.authority.domain.authority.EmployeeUser;
import com.yunhe.authority.domain.authority.Role;
import com.yunhe.authority.domain.company.Employee;
import com.yunhe.authority.service.authority.CompanyRoleService;
import com.yunhe.authority.service.authority.RoleService;
import com.yunhe.authority.service.company.EmployeeService;
import com.yunhe.common.annotation.OperationRecord;
import com.yunhe.common.constant.SystemConsts;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.PageParam;
import com.yunhe.common.model.ResultObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * 企业角色Controller
 * @author liuronglei
 */
@Api(value = "role",description = "企业角色维护", tags = {"Authority_CompanyRole"})
@RestController
@RequestMapping("/companyRole")
public class CompanyRoleController {

    @Autowired
    private CompanyRoleService companyRoleService;

    /**
     * 新增角色(企业)
     * @param companyId  企业ID
     * @param role  角色对象
     * @return
     * @throws ArgumentErrorException
     * @throws UniqueConstraintsException
     * @throws ObjectNotFoundException
     */
    @ApiOperation("新增角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "企业ID", required = true, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @OperationRecord(menu = "角色管理", actionType = SystemConsts.ACTION.ADD)
    public ResultObject<Role> post(@RequestParam(value = "companyId", required = true) Long companyId,
                                   @RequestBody Role role)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        Role newRole = companyRoleService.createRole(companyId, role);
        return new ResultObject<>(newRole);
    }

    /**
     * 更新角色
     * @param companyId 企业ID
     * @param role 角色对象
     */
    @ApiOperation("更新角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "企业ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/update/{companyId}", method = RequestMethod.PUT)
    @OperationRecord(menu = "角色管理", actionType = SystemConsts.ACTION.EDIT)
    public ResultObject<Role> put(@PathVariable("companyId") Long companyId,
                                            @RequestBody Role role)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        Role newRole = companyRoleService.updateRole(companyId, role);
        return new ResultObject<>(newRole);
    }

    /**
     * 根据企业ID获得角色列表(分页)
     * @param companyId 企业ID
     * @param keywords 关键词
     */
    @ApiOperation("根据企业ID获得用户列表(分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "企业ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "keywords", value = "关键词", required = false, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/searchPageByCompanyId", method = RequestMethod.GET)
    public ResultObject<Page<Role>> searchPageByCompanyId(@RequestParam(value = "keywords", required = false) String keywords,
                                                              @RequestParam(value = "companyId", required = false) Long companyId,
                                                              @ModelAttribute PageParam pageParam) {
        Page<Role> rolePage = companyRoleService.findRolesByCompanyIdForPage(companyId, keywords, pageParam);
        return new ResultObject<>(rolePage);
    }

    /**
     * 根据企业ID获得角色列表(不分页)
     * @param companyId 企业ID
     */
    @ApiOperation("根据企业ID获得角色列表(不分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "企业ID", required = true, dataType = "long", paramType = "query")
    })
    @RequestMapping(value = "/searchByCompanyId", method = RequestMethod.GET)
    public ResultObject<Iterable<Role>> searchByCompanyId(@RequestParam(value = "companyId", required = true) Long companyId) {
        Iterable<Role> roleList = companyRoleService.findRolesByCompanyId(companyId);
        return new ResultObject<>(roleList);
    }

}